package cn.study.vue_project_xxx.controller;

import cn.study.vue_project_xxx.common.exception.KnowException;
import cn.study.vue_project_xxx.common.result.ResultSet;
import cn.study.vue_project_xxx.entity.dto.RegisterUserDTO;
import cn.study.vue_project_xxx.entity.po.UserLogin;
import cn.study.vue_project_xxx.service.LoginService;
import cn.study.vue_project_xxx.util.InstanceFactoryUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    LoginService loginService;

    @PostMapping("/toLogin")
    public ResultSet<Boolean> toLogin(@RequestBody RegisterUserDTO registerUserDTO) {
        if (!Objects.isNull(registerUserDTO.getLoginNumber()) && !Objects.isNull(registerUserDTO.getLoginPassword())) {
            UserLogin instance = InstanceFactoryUtil.getInstance(UserLogin.class);
            BeanUtils.copyProperties(registerUserDTO, instance);
            loginService.doLogin(instance);
            return ResultSet.success(true);
        }
        return ResultSet.success(false);
    }

    @PostMapping("/toLogOut")
    public ResultSet<Boolean> toLogOut(Long userId) {
        if (!Objects.isNull(userId)) {
            loginService.doLogOut(userId);
            return ResultSet.success(true);
        }
        throw new KnowException("未指定退出登录用户");
    }

}
